package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.course.bean.PeiXunListBean;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class PeiXunVideoRvAdapter extends RecyclerView.Adapter<PeiXunVideoRvAdapter.PeiXunVideoRvViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<PeiXunListBean.DataBean> dataBeanList = new ArrayList<>();

    public PeiXunVideoRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setDataBeanList(List<PeiXunListBean.DataBean> dataBeanList) {
        this.dataBeanList.addAll(dataBeanList);
        notifyDataSetChanged();
    }

    public List<PeiXunListBean.DataBean> getDataBeanList() {
        return dataBeanList;
    }

    @NonNull
    @Override
    public PeiXunVideoRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.peixun_video_rv_item,viewGroup,false);
        return new PeiXunVideoRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PeiXunVideoRvViewHolder holder, int i) {
        Glide.with(context).load(AppUtils.COURSE_IMAGE_URL + dataBeanList.get(i).getCover()).placeholder(R.mipmap.doc_default).into(holder.img);
        holder.title.setText(dataBeanList.get(i).getName());
        if (TextUtils.isEmpty(dataBeanList.get(i).getBuyCount()+"")){
            holder.classnum.setText("");
        }else {
            holder.classnum.setText("课时:共"+ dataBeanList.get(i).getBuyCount()+"课时");
        }
        String timeExpend = getTimeExpend(dataBeanList.get(i).getStartTime(), dataBeanList.get(i).getEndTime());
        if (TextUtils.isEmpty(timeExpend)){
            holder.classtime.setText("");
        }else {
            String[] split = timeExpend.split(":");
            holder.classtime.setText("时长:"+split[0]+"小时"+split[1]+"分");
        }
        holder.price.setText("¥"+ dataBeanList.get(i).getPrice());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                sb.append(dataBeanList.get(i).getType()+"");
                sb.append(dataBeanList.get(i).getId()+"");
                context.startActivity(CourseDetailActivity.getIntent(context,sb.toString(), dataBeanList.get(i).getCover(),""));
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    class PeiXunVideoRvViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView title,classnum,classtime,price;
        LinearLayout linearLayout;
        public PeiXunVideoRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.peixun_list_img);
            title = itemView.findViewById(R.id.peixun_list_title);
            classnum = itemView.findViewById(R.id.peixun_list_classnum);
            classtime = itemView.findViewById(R.id.peixun_list_classtime);
            price = itemView.findViewById(R.id.peixun_list_price);
            linearLayout = itemView.findViewById(R.id.peixun_list_LinearLayout);
        }
    }


    private String getTimeExpend(long startTime, long endTime){
        //传入字串类型 2016/06/28 08:30
        long longStart = startTime; //获取开始时间毫秒数
        long longEnd = endTime;  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longHours = longExpend / (60 * 60 * 1000); //根据时间差来计算小时数
        long longMinutes = (longExpend - longHours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数

        return longHours + ":" + longMinutes;
    }

}
